package com.ruoyi.web.web.form;

import com.ruoyi.accountant.exception.ValidateException;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/***
 * 会计项目详情表单
 */
@Data
public class ReasonDetailForm implements Validatable {
    private String transactionDate; // 交易日期
    private Integer rows; // 条数
    private Integer page; // 页数
    private String other; // 综合
    private Long ofReason;

    private Date transactionStartDate; // 交易日期开始
    private Date transactionEndDate; // 交易日期结束

    public Validation validate() {
        if(rows == null || rows < 0) {
            rows = 50;
        }
        if(page == null || page < 0) {
            page = 1;
        }

        try {
            if(StringUtils.isEmpty(transactionDate)) {
                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                c.add(Calendar.MONTH, -1);

                transactionDate = simpleDateFormat.format(c.getTime())+"-"+simpleDateFormat.format(new Date());
            }

            if(!StringUtils.isEmpty(transactionDate)) {
                String[] transactionDateValues = transactionDate.split("-");
                this.transactionStartDate = simpleDateFormat.parse(transactionDateValues[0]);
                this.transactionEndDate = simpleDateFormat.parse(transactionDateValues[1]);
            }
        } catch (ParseException e) {
            throw new ValidateException(e);
        }

        return new Validation(true, "OK");
    }
}
